package com.fact.models;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class EmailService {
	
	static Properties mailServerProperties;
    static Session getMailSession;
    static MimeMessage generateMailMessage;
    
	public void sentEmail(InternetAddress[] to_list,InternetAddress[] cc_list,String emailSubject,String emailBody) throws AddressException, MessagingException
	{
	  //Step1  	
        System.out.println("\n 1st ===> setup Mail Server Properties..");		
		mailServerProperties = System.getProperties();
        mailServerProperties.put("mail.smtp.port", "587");
        mailServerProperties.put("mail.smtp.auth", "true");
        mailServerProperties.put("mail.smtp.starttls.enable", "true");
        System.out.println("Mail Server Properties have been setup successfully..");
        
      //Step2        
              System.out.println("\n\n 2nd ===> get Mail Session..");
              getMailSession = Session.getDefaultInstance(mailServerProperties, null);
              generateMailMessage = new MimeMessage(getMailSession);
              //generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("ifticse_kuet@hotmail.com"));
              generateMailMessage.addRecipients(Message.RecipientType.TO, to_list);
              generateMailMessage.addRecipients(Message.RecipientType.CC, cc_list);
              generateMailMessage.setSubject(emailSubject);
              generateMailMessage.setContent(emailBody, "text/html");
              System.out.println("Mail Session has been created successfully..");
       
      //Step3        
              System.out.println("\n\n 3rd ===> Get Session and Send mail");
              Transport transport = getMailSession.getTransport("smtp");
              // Enter your correct gmail UserID and Password
              //transport.connect("smtp.gmail.com", "<-- your gmail USERNAME -->", "<-- your gmail PASSWORD -->");
              transport.connect(System.getProperty("SMTP_HOST_NAME"),System.getProperty("SMTP_AUTH_USER"),System.getProperty("SMTP_AUTH_PWD"));
              transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
              transport.close();
	}
    
    public static void main(String[] args)
    {

		System.setProperty("SMTP_HOST_NAME", "smtp.gmail.com");
		System.setProperty("SMTP_AUTH_USER", "ifticandoit@gmail.com");
		System.setProperty("SMTP_AUTH_PWD", "XXXXXXXXXX");
		
		InternetAddress[] addressTo = new InternetAddress[1];
		InternetAddress[] addressCC = new InternetAddress[1];
		
		
    	EmailService mailService=new EmailService();
    	try
    	{
    		addressTo[0]= new javax.mail.internet.InternetAddress("ifticse_kuet@hotmail.com");		
    		addressCC[0]= new javax.mail.internet.InternetAddress("ifticandoit@gmail.com");
    		
    		mailService.sentEmail(addressTo,addressCC,"Email Subject","Test email by iCrunched JavaMail API example. " + "<br><br> Regards, <br>iCrunched Admin");
    	}
    	catch(Exception ex)
    	{
    		ex.printStackTrace();
    	}
    }

}
